<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/7
 * Time: 8:53
 */

namespace app\admin\controller;


use app\reuse\controller\ResponseJson;
use app\service\MemberService;
use MongoDB\Driver\Manager;
use think\App;
use think\Collection;
use think\Db;

class PlatformData extends Common
{
    use ResponseJson;

    public function __construct(App $app = null)
    {
        parent::__construct();
    }

    /**
     * @return array|mixed|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 意见反馈
     */
    function feedback()
    {
        # 加载页面
        if (input('get.action') == 'view') {
            $status = input('get.read_status');
            $this->assign('read_status', $status);
            return $this->fetch('/platformData/feedback');
        }
        # 查询列表
        if (input('get.action') == 'ajax') {
            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 15;
            }
            # where 条件
            if (!empty(input('post.read_status'))) {
                $_where = 'read_status = ' . input('post.read_status');
            } else {
                $_where = 'id >0 ';
            }
            # 搜索条件
            $searchParams = json_decode(input('post.searchParams'), true);
            if (!empty($searchParams)) {
                # 状态筛选
                if (!empty($searchParams['read_status'])) {
                    $_where = 'read_status = ' . $searchParams['read_status'];
                } else {
                    $_where = 'id >0 ';
                }
                # 姓名/手机号
                if (!empty($searchParams['searchInfo'])) {
                    $_where .= " and name like '%" . $searchParams['searchInfo'] . "%' or mobile like '%" . $searchParams['searchInfo'] . "%'";
                }
                # 时间筛选
                if (!empty($searchParams['searchTime_min'])) {
                    $_where .= " and date(create_time) >= '" . $searchParams['searchTime_min'] . "'";
                }
                if (!empty($searchParams['searchTime_max'])) {
                    $_where .= " and date(create_time) <= '" . $searchParams['searchTime_max'] . "'";
                }
            }
            $info = Db::table('feedback')
                ->field('id,member_code,context,picurl,mobile,name,note,read_status,create_time')
                ->where($_where)
                ->order(array('read_status' => 'asc', 'create_time' => 'desc'))
                ->paginate($listRows, false, ['query' => request()->param()]);
            return $this->jsonSuccessData($info);
        }
        # 删除
        if (input('get.action') == 'delete') {
            $id = input('post.id');
            if (!empty($id)) {
                $res = Db::table('feedback')->where(array('id' => $id))->delete();
                if ($res) {
                    return array('status' => true);
                } else {
                    return array('status' => false, 'msg' => '删除失败');
                }
            } else {
                return array('status' => false, 'msg' => '缺少参数');
            }
        }
        # 查看详情
        if (input('get.action') == 'detail') {
            $id = input('get.id');
            $info = Db::table('feedback')
                ->field('id,context,picurl,mobile,name,note,reply_info,member_code')
                ->where(array('id' => $id))
                ->find();
            $info['picurl'] = explode(',', $info['picurl']);
            $this->assign('info', $info);
            return $this->fetch('/platformData/feedback_detail');
        }
        # 回复
        if (input('get.action') == 'reply') {
            $info = input('post.');
            Db::table('feedback')
                ->where(array('id' => $info['id']))
                ->update(array('note' => $info['note'], 'reply_info' => $info['reply_info'], 'reply_time' => date("Y-m-d H:i:s"), 'read_status' => 2));
            return array('status' => true);
        }
    }

    /**
     * @return array|mixed|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 交易审核
     */
    function tradAudit()
    {
        # 加载页面
        if (input('get.action') == 'view') {
            $this->assign("lanunchdetail", input("get.lanunchdetail"));
            $this->assign("member_phone", input("get.member_phone"));
            $this->assign("source", input("get.source"));
            $this->assign("buy_code", input("get.buy_code"));
            $this->assign("business_id", input("get.business_id"));
            return $this->fetch('/platformData/tradAudit');
        }
        # 查询列表
        if (input('get.action') == 'ajax') {
            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 15;
            }
            # where 条件
            $_where = 'ta.del_status = 1';

            # 搜索条件
            $searchParams = json_decode(input('post.searchParams'), true);
            if (empty($searchParams)) {
                $_where .= ' and trad_status = 1';
            }
            $lanunchdetail = input("get.lanunchdetail");
            if (!empty($lanunchdetail)) {
                $member_phone = input("get.member_phone");
                $_where = "(sell_phone = '{$member_phone}')";
            }
            $source = input("get.source");
            if (!empty($source)) {
                $buy_code = input("get.buy_code");
                $business_id = input("get.business_id");
                $_where = " ta.buy_code='{$buy_code}' and buyer_business_id = {$business_id}";
            }
            if (!empty($searchParams)) {
                # 状态筛选
                if (!empty($searchParams['trad_status'])) {
                    $_where .= ' and trad_status = ' . $searchParams['trad_status'];
                } else {
                    $_where .= ' and trad_status = 1';
                }
                # 姓名/手机号
                if (!empty($searchParams['searchInfo'])) {
                    $_where .= " and (buy_name like '%" . $searchParams['searchInfo'] . "%' or buy_phone like '%" . $searchParams['searchInfo'] . "%'"
                        . " or sell_name like '%" . $searchParams['searchInfo'] . "%' or sell_phone like '%" . $searchParams['searchInfo'] . "%')";
                }
                # 时间筛选
                if (!empty($searchParams['searchTime_min'])) {
                    $_where .= " and date(trad_time) >= '" . $searchParams['searchTime_min'] . "'";
                }
                if (!empty($searchParams['searchTime_max'])) {
                    $_where .= " and date(trad_time) <= '" . $searchParams['searchTime_max'] . "'";
                }
            }

            $info = Db::table('trad_audit ta')
                ->field('ta.id,ta.buy_code,buy_name,buy_phone,ta.industry_id,price,screenshot,sell_code,sell_name,sell_phone,trad_time,trad_status,industry_title')
                ->join('member_business mb', 'ta.industry_id=mb.id', 'left')
                ->where($_where)
                ->order(array('trad_time' => 'desc'))
                ->paginate($listRows, false, ['query' => request()->param()]);
            return $this->jsonSuccessData($info);
        }
        # 详情
        if (input('get.action') == 'detail') {
            $id = input('get.id');
            $info = Db::table('trad_audit ta')
                ->field('ta.id,ta.buy_code,buy_name,buy_phone,ta.industry_id,price,screenshot,sell_code,sell_name,sell_phone,trad_time,trad_status,
                industry_title,il.title levelTitle,is_violations,ta.voucher_title,ta.voucher_price')
                ->join('member_business mb', 'ta.industry_id=mb.id', 'left')
                ->join('industry_level il', 'il.id=mb.earnings_level', 'left')
                ->where(array('ta.id' => $id))
                ->find();
            $info['screenshot'] = explode(',', $info['screenshot']);
            $this->assign('info', $info);
            return $this->fetch('/platformData/tradAudit_detail');
        }
        # 违规判定
        if (input('get.action') == 'violations') {
            $tradAudit_id = input('post.id');
            $_res = MemberService::violationJudgment($tradAudit_id);
            return array('status' => $_res['status'], 'msg' => $_res['msg']);
        }
        # 删除
        if (input('get.action') == 'delete') {
            $tradAudit_id = input('post.id');
            Db::table('trad_audit')->where(array('id' => $tradAudit_id))->update(array('del_status' => 2));
            return array('status' => true);
        }
    }

    /**
     * @return array|mixed|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 玩家投诉
     */
    function complaint()
    {
        # 加载页面
        if (input('get.action') == 'view') {
            return $this->fetch('/platformData/complaint');
        }
        # 查询列表
        if (input('get.action') == 'ajax') {
            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 15;
            }
            # where 条件
            $_where = 'status != -1';
            # 搜索条件
            $searchParams = json_decode(input('post.searchParams'), true);
            if (!empty($searchParams)) {
                # 状态筛选
                if (!empty($searchParams['status'])) {
                    if ($searchParams['status'] == 1) {
                        # 待处理
                        $_where .= ' and status = 1';
                    } else {
                        # 已处理
                        $_where .= ' and status != 1';
                    }
                }
                # 姓名/手机号
                if (!empty($searchParams['searchInfo'])) {
                    $_where .= " and sell_name like '%" . $searchParams['searchInfo'] . "%' or sell_phone like '%" . $searchParams['searchInfo'] . "%'";
                }
                # 时间筛选
                if (!empty($searchParams['searchTime_min'])) {
                    $_where .= " and date(create_time) >= '" . $searchParams['searchTime_min'] . "'";
                }
                if (!empty($searchParams['searchTime_max'])) {
                    $_where .= " and date(create_time) <= '" . $searchParams['searchTime_max'] . "'";
                }
            }
            $info = Db::table('complaint')
                ->field('id,buy_code,buy_name,buy_phone,industry_id,screenshot,sell_code,sell_name,sell_phone,create_time,status,
                reason,complaint_context,type_mark')
                ->where($_where)
                ->order(array('create_time' => 'desc'))
                ->paginate($listRows, false, ['query' => request()->param()]);
            return $this->jsonSuccessData($info);
        }
        # 查看详情
        if (input('get.action') == 'detail') {
            $id = input('get.id');
            $info = Db::table('complaint')
                ->field('id,buy_code,buy_name,buy_phone,industry_id,screenshot,sell_code,sell_name,sell_phone,create_time,status,handle_status,
                reason,complaint_context,type_mark,trad_audit_id')
                ->where(array('id' => $id))
                ->find();
            $info['screenshot'] = explode(',', $info['screenshot']);
            if ($info['type_mark'] == 2) {
                # 交易厅投诉 , 查询交易厅信息
                $tradingInfo = Db::table('trading_room')->where(array('id' => $info['trad_audit_id']))->find();
                $this->assign('tradingInfo', $tradingInfo);
            }
            $this->assign('info', $info);
            return $this->fetch('/platformData/complaint_detail');
        }
        # 审核操作
        if (input('get.action') == 'violations') {
            $id = input('post.id');
            # 审核类型 adopt:审核通过  reject:审核驳回
            $type = input('post.type');
            if ($type == 'adopt') {
                # 审核通过
                $_res = MemberService::violationJudgment($id, 'complaint');
                return array('status' => $_res['status'], 'msg' => $_res['msg']);
            } else {
                # 审核驳回
                # 只需要修改审核状态
                Db::table('complaint')->where(array('id' => $id))->update(array('status' => 2, 'handle_status' => 2, 'handle_time' => date("Y-m-d H:i:s")));
                $info = Db::table('complaint')
                    ->field('id,trad_audit_id,type_mark')
                    ->where(array('id' => $id))
                    ->find();
                if ($info['type_mark'] == 2) {
                    # 审核驳回
                    Db::table('trading_room')->where(array('id' => $info['trad_audit_id']))->update(array('report_status' => 4));
                }
                return array('status' => true);
            }
        }
        # 删除
        if (input('get.action') == 'delete') {
            $id = input('post.id');
            if (!empty($id)) {
                $res = Db::table('complaint')->where(array('id' => $id))->update(array('status' => -1));
                if ($res) {
                    return array('status' => true);
                } else {
                    return array('status' => false, 'msg' => '删除失败');
                }
            } else {
                return array('status' => false, 'msg' => '缺少参数');
            }
        }
    }

    /**
     * @return array|mixed|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 合伙人申请
     */
    function partner()
    {
        # 加载页面
        if (input('get.action') == 'view') {
            return $this->fetch('/platformData/partner');
        }
        # 查询列表
        if (input('get.action') == 'ajax') {
            # 定义listRows
            $listRows = input('post.limit');
            if (empty($listRows)) {
                $listRows = 15;
            }
            # where 条件
            $_where = 'status != -1';
            # 搜索条件
            $searchParams = json_decode(input('post.searchParams'), true);
            if (!empty($searchParams)) {
                # 状态筛选
                if (!empty($searchParams['status'])) {
                    $_where .= ' and status in( ' . $searchParams['status'] . ")";
                }
                # 姓名/手机号/ID
                if (!empty($searchParams['searchInfo'])) {
                    $_where .= " and name like '%" . $searchParams['searchInfo'] . "%' 
                    or phone like '%" . $searchParams['searchInfo'] . "%'
                    or member_code = '" . $searchParams['searchInfo'] . "'
                    ";
                }
                # 时间筛选
                if (!empty($searchParams['searchTime_min'])) {
                    $_where .= " and date(create_time) >= '" . $searchParams['searchTime_min'] . "'";
                }
                if (!empty($searchParams['searchTime_max'])) {
                    $_where .= " and date(create_time) <= '" . $searchParams['searchTime_max'] . "'";
                }
            }
            $info = Db::table('partner_audit pa')
                ->field('pa.id,pa.name,pa.member_code,pa.phone,pa.create_time,pa.status,pa.note,ml.title')
                ->join('member_level ml', 'ml.level=pa.member_level', 'left')
                ->where($_where)
                ->order(array('create_time' => 'desc'))
                ->paginate($listRows, false, ['query' => request()->param()]);
            return $this->jsonSuccessData($info);
        }
        # 查看详情
        if (input('get.action') == 'detail') {
            $id = input('get.id');
            $info = Db::table('partner_audit pa')
                ->field('pa.id,pa.name,pa.member_code,pa.phone,pa.create_time,pa.status,pa.note,ml.title')
                ->join('member_level ml', 'ml.level=pa.member_level', 'left')
                ->where(array('pa.id' => $id))
                ->find();
            $this->assign('info', $info);
            return $this->fetch('/platformData/partner_detail');
        }
        # 审核操作
        if (input('get.action') == 'operation') {
            $info = input('post.');
            if ($info['status'] != 0) {
                Db::table('partner_audit')->where(array('id' => $info['id']))
                    ->update(array('note' => $info['note'], 'status' => $info['status'], 'update_time' => date("Y-m-d H:i:s")));
                /*if ($info['status'] == 2) {
                    # 审核通过,修改玩家状态为合伙人
                    Db::table('member')->where(array('code' => $info['member_code']))->update(array('type' => 2));
                }*/
            } else {
                Db::table('partner_audit')->where(array('id' => $info['id']))
                    ->update(array('note' => $info['note']));
            }
            return array('status' => true);
        }
        # 删除
        if (input('get.action') == 'delete') {
            $id = input('post.id');
            Db::table('partner_audit')->where(array('id' => $id))->update(array('status' => -1));
            return array('status' => true);
        }
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 当前在线人数-各时段登录总和
     */
    function onlinePeople()
    {
        if (input('get.action') != 'ajax') {
            return $this->fetch('/platformData/onlinePeople');
        } else {
            # 起始时间
            $where = date('Y-m-d');
            $min = input('post.min');
            if (!empty($min)) {
                $where = $min;
            }
            # 在线人数K线统计
            $info = Db::table('log_login')->field('count(id) value')
                ->join('(select 1 as time union
            select 2 as time union select 3 as time union 
            select 4 as time union select 5 as time union 
            select 6 as time union select 7 as time union 
            select 8 as time union select 9 as time union 
            select 10 as time union select 11 as time union 
            select 12 as time union select 13 as time union 
            select 14 as time union select 15 as time union 
            select 16 as time union select 17 as time union 
            select 18 as time union select 19 as time union 
            select 20 as time union select 21 as time union 
            select 22 as time union select 23 as time union 
            select 24 as time) as timeInfo',
                    'timeInfo.time=date_format(log_login.create_time,"%H") 
                and  date(create_time) = "' . $where . '"', 'right')
                ->group('timeInfo.time')
                ->order('timeInfo.time asc')
                ->select();
            $dailyData = array_column($info, 'value');
            $dailyDataCount = array_sum($dailyData);
//            dump($dailyData);
            return array('status' => true, 'dailyData' => $dailyData, 'dailyDataCount' => $dailyDataCount);

        }
    }

    /**
     * @return array|mixed
     * @throws \think\Exception
     * @content 日下载量统计
     */
    function downloadInfo()
    {
        if (input('get.action') != 'ajax') {
            return $this->fetch('/platformData/downloadInfo');
        } else {
            # 起始时间
            $where = date('Y-m-d');
            $min = input('post.min');
            if (!empty($min)) {
                $where = $min;
            }
            $dailyDataCount = 0;
            $dailyData = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
            # 在线人数K线统计
            $info = Db::connect("db_config2")->table("download_info")
//                ->where('date(download_info.time) = "' . $where . '"')
                ->where([['time', '>=', date('Y-m-d 00:00:00', strtotime($where))], ['time', '<=', date('Y-m-d 23:59:59', strtotime($where))]])
                ->select();
            if (!empty($info)) {
                $dailyDataCount = count($info);
                foreach ($info as $k => $v) {
                    $t = intval(date('H', strtotime($v['time'])));
                    $dailyData[$t] = $dailyData[$t] + 1;
                }
            }
            return array('status' => true, 'dailyData' => $dailyData, 'dailyDataCount' => $dailyDataCount);

        }
    }
}